package Dao.impl;
import BaseQuery.OrderQuery;
import BaseQuery.BaseQuery;
import Bean.Order;
import Dao.IOrderDao;
import Utils.JdbcUtils;
import com.alibaba.druid.util.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import vo.PageBean;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDao implements IOrderDao {
	QueryRunner queryRunner;
	public OrderDao( ) {
		queryRunner= JdbcUtils.getQueryRunner();
	}
	@Override
	public int add(Order order) throws SQLException {
		String sql="insert into t_orders (userId,businessId,orderDate, orderTotal,daId, orderState) values(?,?,?,?,?,?)";
		return queryRunner.update(sql,
				order.getUserId(),
				order.getBusinessId(),
				order.getOrderDate(),
				order.getOrderTotal(),
				order.getDaId(),
				order.getOrderState());
	}

	@Override
	public int update(Order order) throws SQLException {
		System.out.println("111111111111111111111111111");
		String sql = "update t_orders set userId=?, businessId=?, orderDate=?, orderTotal=?, daId=?, orderState=? where orderId=?";
		return queryRunner.update(sql,
				order.getUserId(),
				order.getBusinessId(),
				order.getOrderDate(),
				order.getOrderTotal(),
				order.getDaId(),
				order.getOrderState(),
				order.getOrderId());
	}

	@Override
	public int delete(Integer id) throws SQLException {
		String sql="delete from t_orders where orderId=?";
		return queryRunner.update(sql,id);
	}

	@Override
	public Order getById(Integer id) throws SQLException {
		String sql="select orderId,userId,businessId,orderDate,orderTotal,daId,orderState from t_orders where orderId=?";
		return queryRunner.query(sql,new BeanHandler<>(Order.class),id);
	}

	@Override
	public List<Order> getAll() throws SQLException {
		String sql="select orderId,userId,businessId,orderDate,orderTotal,daId,orderState from t_orders ";
		return queryRunner.query(sql,new BeanListHandler<>(Order.class));
	}

	@Override
	public int getCount(BaseQuery baseQuery) throws SQLException {
		List<Object> params=new ArrayList<>();
		OrderQuery query=(OrderQuery) baseQuery;
		//模糊查询
		String sql="select count(orderId) from t_orders where 1=1";
		if(query.getOrderId()!=null && query.getOrderId()!=0) {
			sql+=" and orderId=?";
			params.add(query.getOrderId());
		}
		return queryRunner.query(sql,new ScalarHandler<Long>(),params.toArray()).intValue();
	}

	@Override
	public List<Order> getByPage(BaseQuery baseQuery) throws SQLException {
		List<Object> params=new ArrayList<>();
		OrderQuery query=(OrderQuery) baseQuery;
		System.out.println("==========================");
		System.out.println(query.getOrderId());
		String sql="select orderId,userId,businessId,orderDate,orderTotal,daId,orderState from t_orders where 1=1";
		if(query.getOrderId()!=null && query.getOrderId()!=0) {
			sql+=" and orderId=?";
			params.add(query.getOrderId());
		}
		sql+=" ORDER BY orderId DESC limit ?,?";
		params.add(query.getstartIndex());
		params.add(query.getPageCount());
		System.out.println("查询语句："+sql);
		System.out.println("集合合集："+params);
		return queryRunner.query(sql,new BeanListHandler<>(Order.class),params.toArray());
	}
}
